﻿using System;
using System.IO.IsolatedStorage;
using System.Diagnostics;
using System.Collections.Generic;
using BitcoinMarket.Resources;
using System.Collections.ObjectModel;

namespace BitcoinMarket
{
    public class CurrencySettingsItem
    {
        public string currencyTitle { get; set; }
        public bool toppedDefault { get; set; }
    }

    public class AppCurrencySettings
    {
        // Our settings
        IsolatedStorageSettings settings;

        //collection of items to show
        Collection<CurrencySettingsItem> currencyItems = null;

        /// <summary>
        /// Constructor that gets the application settings.
        /// </summary>
        public AppCurrencySettings()
        {
            //Create the list of available settings
            this.currencyItems = new Collection<CurrencySettingsItem>();

            this.currencyItems.Add(new CurrencySettingsItem { currencyTitle = "USD", toppedDefault = true });
            this.currencyItems.Add(new CurrencySettingsItem { currencyTitle = "EUR", toppedDefault = true });
            
            // Get the settings for this application.
            settings = IsolatedStorageSettings.ApplicationSettings;
        }

        /// <summary>
        /// Returns the list of available network info chart items
        /// </summary>
        /// <returns></returns>
        public Collection<CurrencySettingsItem> GetAllCurrencyItems()
        {
            return this.currencyItems;
        }

        public bool GetToppedDefault(string CurrencyName)
        {
            bool dflt = false;

            foreach (CurrencySettingsItem it in this.currencyItems)
            {
                if (it.currencyTitle.Equals(CurrencyName))
                {
                    dflt = it.toppedDefault;
                    break;
                }
            }

            return dflt;
        }

        /// <summary>
        /// Update a setting value for our application. If the setting does not
        /// exist, then add the setting.
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool AddOrUpdateValue(string Key, Object value)
        {
            bool valueChanged = false;
            Key = "curr_" + Key;

            // If the key exists
            if (settings.Contains(Key))
            {
                // If the value has changed
                if (settings[Key] != value)
                {
                    // Store the new value
                    settings[Key] = value;
                    valueChanged = true;
                }
            }
            // Otherwise create the key.
            else
            {
                settings.Add(Key, value);
                valueChanged = true;
            }

            Save();

            return valueChanged;
        }

        /// <summary>
        /// Get the current value of the setting, or if it is not found, set the 
        /// setting to the default setting.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public T GetValueOrDefault<T>(string Key, T defaultValue)
        {
            T value;
            Key = "curr_" + Key;

            // If the key exists, retrieve the value.
            if (settings.Contains(Key))
            {
                value = (T)settings[Key];
            }
            // Otherwise, use the default value.
            else
            {
                value = defaultValue;
            }
            return value;
        }

        /// <summary>
        /// Save the settings.
        /// </summary>
        public void Save()
        {
            settings.Save();

            // Reload the charts
            App.ViewModel.LoadMarketsData();
        }
    }
}
